<?php
namespace Admin\Controller;
use Think\Controller;
//供应商控制器 by chenheyou
class FormController extends BaseController {
	
	//2016.4.14 by chenheyou
    public function index(){
        $object=M('Form');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		$list=$object->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign("page",$show);
        $this->display();
    }
	
	//2016.4.14 by chenheyou
    public function editUI(){
		if(I('get.id')){
			$this->assign('object',M('Form')->where('id='.I('get.id'))->find());
			$this->assign('fieldList',M('Formfield')->where('formid='.I('get.id'))->select());
		}
        $this->display('editUI');
    }
	
	//2016.4.14 by chenheyou
    public function editOperation(){
		$Object = M("Form");   
		$Object->create();
		$formname = $Object->formname;
		$id = I('post.id');
		$dbnameArray 	= I('post.dbname');     //数据库字段列名
		$fieldnameArray = I('post.fieldname');  //显示名
		$fieldidArray   = I('post.fieldid');    //id
		
		if(!$id){//如果没有id
		    $obj = M('Form')->where("formname='".$formname."'")->find();
		    if($obj){
				$this->error("<script>alert('添加失败,表名重复');</script>");
		        return;
		    }
		    $Object->modifytime=time();
			if($id=$Object->add()){
				$this->success('添加成功',U('index'));
				$Object1 = M("Formfield");
				$sql = "";
				for ($i=0;$i<count($dbnameArray);$i++){
					$Object1 -> formid = $id;
					$Object1 -> dbname = $dbnameArray[$i];
					$Object1 -> fieldname = $fieldnameArray[$i];
					$sql = $sql."`".$Object1->dbname."` varchar(50) DEFAULT NULL,";
					$Object1->add();
				}
				$sql = "CREATE TABLE `bs_".$formname."` (".
					"`id` int(11) NOT NULL AUTO_INCREMENT,".
					"`moduleId` int(11) ,".
					"`requestId` int(11) ,".
					"`creater` int(11) ,".
					$sql.
					"PRIMARY KEY (`id`)".
					") ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;";
				M() -> query($sql);
			}else{
				dump($Object->_sql());
				$this->error('添加失败');
			}
		}else{//如果是修改
		    $Object->modifytime=time();
			if($Object->save()){
				$Object1 = M("Formfield");
				for ($i=0;$i<count($dbnameArray);$i++){
				    $Object1 -> id     = $fieldidArray[$i];
					$Object1 -> formid = $id;
					$Object1 -> dbname = $dbnameArray[$i];
					$Object1 -> fieldname = $fieldnameArray[$i];
					$Object1->save();
				}
				$this->success('修改成功',U('index'));
			}else{
				$this->error('修改失败');
			}
		}
    }
	
	//2016.4.14 by chenheyou
    public function removeOperation(){
		// 获取表单的POST数据
		$Object = M("Form"); // 实例化User对象
		$id = I('get.id');
		if($Object->delete($id)){
			$this->success('删除成功',U('index'));
			M("Formfield")->where('formid='.$id)->delete();
			M() -> query("DROP table ".M('Form')->where('id='.I('get.id'))->find()->formname);
		}else{
			dump($Object->_sql());
			$this->error('删除失败');
		}
    }
	
	//2016.4.14 by chenheyou
    public function infoUI(){
        $this->display('info');
    }
}